﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using Core;

namespace TestProject
{
    public partial class NewLinkTable : Form
    {
        private MainWindow mw;
        public NewLinkTable(MainWindow mw)
        {
            InitializeComponent();
            this.mw=mw;
        }

        private void create_Click(object sender, EventArgs e)
        {            
            if (type_box.SelectedItem.ToString() == "chromosome_and_gene")
            {
                XmlTextWriter writer = new XmlTextWriter("file_input\\" + mw.model.model_name + "\\Links\\ChromosomeGeneLinks\\" + name_box.Text + ".xml", null);
                writer.Formatting = Formatting.Indented;
                writer.WriteStartDocument();
                writer.WriteStartElement("link");
                writer.WriteAttributeString("Type", "chromosome_and_gene");
                writer.WriteAttributeString("Name", name_box.Text);
                writer.WriteAttributeString("PathToChromosomeTable", chrom_path_box.Text);
                writer.WriteAttributeString("PathToGeneTable", gene_path_box.Text);
                writer.WriteAttributeString("ChromosomePairsAmount", pairs_box.Text);
                writer.WriteStartElement("chromosomes");
                if ((pairs_box.Text != null) && (pairs_box.Text != "0"))
                {
                    for (int i = 0; i < Convert.ToInt32(pairs_box.Text); i++)
                    {
                        writer.WriteStartElement("chromosome");
                        writer.WriteAttributeString("LocusesAmount", "0");
                        writer.WriteStartElement("gene_categories");
                        writer.WriteEndElement();
                        writer.WriteEndElement();
                    }
                }
                writer.WriteEndElement();
                writer.WriteEndElement();
                writer.WriteEndDocument();
                writer.Close();
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(System.IO.File.ReadAllText(mw.model.model_path));
                XmlElement el;
                el = doc.CreateElement("link");
                el.SetAttribute("Path", "file_input\\" + mw.model.model_name + "\\Links\\ChromosomeGeneLinks\\" + name_box.Text + ".xml");
                doc.GetElementsByTagName("links")[0].Attributes[0].Value = (Convert.ToInt32(doc.GetElementsByTagName("links")[0].Attributes[0].Value) + 1).ToString();
                doc.DocumentElement.SetAttribute("AmountLinks", (Convert.ToInt32(doc.DocumentElement.GetAttribute("AmountLinks")) + 1).ToString());
                doc.GetElementsByTagName("chromosome_and_gene_links")[0].AppendChild(el);
                doc.Save(mw.model.model_path);
                mw.treeView1.Nodes[0].Nodes[1].Nodes.Add(name_box.Text);                
            }
            this.Close();
        }

        private void chrom_browse_Click(object sender, EventArgs e)
        {
            OpenFileDialog op = new OpenFileDialog();
            op.InitialDirectory = Environment.CurrentDirectory+"\\file_input";
            if (op.ShowDialog() == DialogResult.OK)
            {
                chrom_path_box.Text = op.FileName;
            }
        }

        private void gene_browse_Click(object sender, EventArgs e)
        {
            OpenFileDialog op = new OpenFileDialog();
            op.InitialDirectory = Environment.CurrentDirectory + "\\file_input";
            if (op.ShowDialog() == DialogResult.OK)
            {
                gene_path_box.Text = op.FileName;
            }
        }

        private void canc_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
